package com.ck.ck_back.service.impl;

import com.ck.ck_back.dao.HistoryMapper;
import com.ck.ck_back.dao.ProductionMapper;
import com.ck.ck_back.dao.UserMapper;
import com.ck.ck_back.pojo.History;
import com.ck.ck_back.pojo.Production;
import com.ck.ck_back.pojo.User;
import com.ck.ck_back.service.historyservice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.ws.Holder;
import java.util.Date;
import java.util.List;

@Service
public class historyserviceImpl implements historyservice {
    @Resource
    private UserMapper userMapper;
    @Resource
    private ProductionMapper productionMapper;
    @Resource
    private HistoryMapper historyMapper;

//    增加历史记录，404为无此人，403无此作品，1成功，2失败
    @Transactional
    @Override
    public int addhistory(String openid,int production_id) {
        User user = userMapper.selectByPrimaryKey(openid);
        if (user != null){
            Production production = productionMapper.selectByPrimaryKey(production_id);
            if (production!=null){
                History history = new History();
                history.setUserId(openid);
                history.setProductionId(production_id);
                history.setHistoryTime(new Date().getTime());
                int result = historyMapper.insertSelective(history);
                if (result == 1){
                    return result;
                }else {return 2;}
            }else {return 403;}
        }else{return 404;}
    }

//    删除历史（将state修改为0），404，参数错误，3无此历史，2删除错误，1成功
    @Transactional
    @Override
    public int updatehistory(String user_id,int production_id) {
        if(!String.valueOf(production_id).equals("") &&user_id!=null &&!user_id.equals("")){
            History history = historyMapper.selectbytwoid(user_id,production_id);
            if (history!= null){
                history.setState(0);
                int result = historyMapper.updateByPrimaryKeySelective(history);
                if (result == 1){
                    return result;
                }else {return 2;}
            }else{
                return 3;
            }
        }else {return 404;}
    }

    @Override
    public int get_history_num(String user_id, int production_id) {
        return historyMapper.get_history_num(user_id,production_id);
    }

    @Override
    public List<History> getall(String user_id) {
        return historyMapper.selectall(user_id);
    }

    @Override
    public int deletehistory(int historyId) {
        if (!String.valueOf(historyId).equals("")){
            History history = historyMapper.selectByPrimaryKey(historyId);
            if (history!=null){
                history.setState(0);
                int result = historyMapper.updateByPrimaryKeySelective(history);
                if (result == 1){
                    return result;
                }else {
                    return 2;
                }
            }else {return 3;}
        }else {return 404;}
    }


}
